package com.example.canvasdemo.network

import retrofit2.Response
import com.example.canvasdemo.model.ChatMessage
import com.example.canvasdemo.model.CreateRoomResponse
import com.example.canvasdemo.model.DrawingModel
import com.example.canvasdemo.model.DrawingResponse
import com.example.canvasdemo.model.LoginRequest
import com.example.canvasdemo.model.LoginResponse
import com.example.canvasdemo.model.RegisterRequest
import com.example.canvasdemo.model.RegisterResponse
import com.example.canvasdemo.model.RoleChangeRequest
import com.example.canvasdemo.model.Room
import com.example.canvasdemo.model.SendMessageRequest
import com.example.canvasdemo.model.UpdateResponse
import com.example.canvasdemo.model.UpdateUserRequest
import com.example.canvasdemo.model.UploadResponse
import com.example.canvasdemo.model.UserModel
import com.example.canvasdemo.model.Word
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.Body
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.Field
import retrofit2.http.FormUrlEncoded
import retrofit2.http.Path
import retrofit2.http.Query

interface ApiService {

    // 角色变更接口
    @POST("/room/{roomId}/users/{userId}/change-role")
    fun changeUserRole(
        @Path("roomId") roomId: Long,
        @Path("userId") userId: Long,
        @Body roleChangeRequest: RoleChangeRequest
    ): Call<Map<String, Any>>

    @GET("room/{roomId}")
    fun getRoomStatus(@Path("roomId") roomId: Long): Call<Room>


    // 创建房间
    @FormUrlEncoded
    @POST("/room/create")
    fun createRoom(
        @Field("name") name: String,
        @Field("capacity") capacity: Int
    ): Call<CreateRoomResponse>

    // 获取房间列表
    @GET("/room/list")
    fun getRoomList(): Call<List<Room>>

    @POST("/room/{roomId}/join")
    fun joinRoom(
        @Path("roomId") roomId: Long,
        @Body userId: Map<String, Long>
    ): Call<Map<String, Any>>

    // 根据房间名搜索房间
    @GET("/room/search")
    fun searchRoomByName(@Query("name") name: String): Call<List<Room>>

    @POST("/room/{roomId}/messages")
    fun sendMessage(
        @Path("roomId") roomId: Int,
        @Body request: SendMessageRequest
    ): Call<ChatMessage>

    @GET("/room/{roomId}/messages")
    fun getMessages(@Path("roomId") roomId: Long): Call<List<ChatMessage>>

    @GET("user/{id}")
    fun getUserById(@Path("id") userId: Long): Call<UserModel>

    @POST("user/{id}/update")
    fun updateUser(
        @Path("id") id: Long,
        @Body updateRequest: UpdateUserRequest
    ): Call<UpdateResponse>

    @GET("/game/words")
    fun getWords(@Query("count") count: Int): Call<List<Word>>

    // 上传画作并提交猜测
    @POST("/room/{roomId}/drawings/upload")
    suspend fun uploadDrawing(
        @Path("roomId") roomId: Long, // 动态传入房间ID
        @Body drawingRequest: DrawingModel
    ): Response<UploadResponse>

    @GET("/room/{roomId}/drawings/latest")
    fun getLastDrawingAndHint(@Path("roomId") roomId: Long): Call<DrawingResponse>

    // 用户注册（通过查询参数传递数据）
    @POST("/user/register")
    fun registerUser(@Body request: RegisterRequest): Call<RegisterResponse>

    // 用户登录
    @POST("/user/login")
    fun loginUser(@Body request: LoginRequest): Call<LoginResponse>
}


object RetrofitClient {
    private const val BASE_URL = "http://10.0.2.2:8080"

    private val loggingInterceptor = HttpLoggingInterceptor().apply {
        level = HttpLoggingInterceptor.Level.BODY
    }

    private val client = OkHttpClient.Builder()
        .addInterceptor(loggingInterceptor)
        .build()

    val instance: Retrofit by lazy {
        Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .client(client)
            .build()
    }
}


